package com.itheima.interceptor;


import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//配置拦截器
//实现HandlerInterceptor接口 覆盖HandlerInterceptor其中的方法
public class MyInterceptor implements HandlerInterceptor {

//   前置
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//释放静态资源
        System.out.println("do");
        if (handler instanceof DefaultServletHttpRequestHandler) {
            return true;
        }

//        return false为拦截  true为放行
//        return false;
        return true;
    }

    //后置
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("1213postHandle");
         modelAndView.setViewName("main");
    }

    //完成拦截后运行的方法
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion 1111");
    }
}
